<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="FormData" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">

			<el-form-item label="门店名称" prop="name">
				<el-input v-model="FormData.name" placeholder="请输入门店名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="所属单位" prop="unit">
				<Unit v-model="FormData.unit" placeholder="请选择所属单位"></Unit>
			</el-form-item>
			<el-row :gutter="20">
				<el-col :span="8">
					<el-form-item label="门店编号" prop="code">
						<el-input v-model="FormData.code" placeholder="请输入门店编号" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="门店等级" prop="grade">
						<el-input v-model="FormData.grade" placeholder="请输入门店等级" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="门店面积" prop="area">
						<el-input v-model="FormData.area" placeholder="请输入门店面积" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="8">
					<el-form-item label="联系人" prop="linkName">
						<el-input v-model="FormData.linkName" placeholder="请输入联系人" clearable></el-input>
					</el-form-item>

				</el-col>
				<el-col :span="8">
					<el-form-item label="联系电话" prop="linkTel">
						<el-input v-model="FormData.linkTel" placeholder="请输入联系电话" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="所属区域" prop="sex">
						<el-cascader v-model="FormData.region" :props="areaProps" :options="aeraList"
							@change="handleChange" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="营业时间" prop="businessStart">
				<el-time-select v-model="FormData.businessStart" :max-time="endTime" class="mr-4" placeholder="开始营业"
					start="00:00" step="00:30" end="23:30" />
				<span class="u-m-l-10 u-m-r-10">至</span>
				<el-time-select v-model="FormData.businessEnd" :min-time="startTime" placeholder="停止营业" start="00:00"
					step="00:30" end="23:30" />
			</el-form-item>
			<el-form-item label="详细地址" prop="address">
				<div class="u-flex u-row-between" style="width:100%">
					<el-input v-model="FormData.address" placeholder="请输入详细地址" clearable
						@change="showAMap = true"></el-input>
					<el-button class="u-m-l-20" @click="showAMap = true">地图标注</el-button>
				</div>
			</el-form-item>
			<!-- <el-form-item label="经度" prop="longitude">
				<el-input v-model="FormData.longitude" placeholder="请输入经度" clearable></el-input>
			</el-form-item>
			<el-form-item label="纬度" prop="latitude">
				<el-input v-model="FormData.latitude" placeholder="请输入纬度" clearable></el-input>
			</el-form-item> -->
			<div v-if="showAMap" style="margin:0 0 10px 100px">
				<AMap :bmapSearMsg="FormData.address" :longAndLat="FormData.longAndLat" @comfirmBmap="comfirmBmap">
				</AMap>
			</div>
			<!-- <el-form-item label="排序" prop="num">
					<el-input-number v-model="FormData.num" :min="1" controls-position="right" style="width: 20%;"></el-input-number>
				</el-form-item> -->
			<!-- <el-form-item label="门店状态" prop="boolean">
				<el-radio-group v-model="FormData.sex">
					<el-radio label="开业">开业</el-radio>
					<el-radio label="停业">停业</el-radio>
				</el-radio-group>
			</el-form-item> -->
		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				FormData: {
					name: null,
					unit: {}
				},
				//验证规则
				rules: {
					name: [{
						required: true,
						message: '请输入姓名'
					}]
				},
				areaProps: {
					value: 'id',
					label: 'name',
					checkStrictly: false
				},
				aeraList: []
			}
		},
		watch: {
			// 'FormData.address'(nVal, oVal) {
			// 	if (nVal == oVal) {
			// 		this.showAMap = false
			// 	} else {
			// 		this.showAMap = true
			// 	}
			// }
		},
		mounted() {
			this.getArea()
		},
		methods: {
			//加载树数据
			async getArea() {
				const res = await this.$API.system.area.tree.post()
				this.aeraList = res.data
			},
			// 回传经纬度
			comfirmBmap(mapData) {
				console.log(mapData)
				this.FormData.longitude = String(mapData.lng)
				this.FormData.latitude = String(mapData.lat)
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true;

				if (this.FormData.region && this.FormData.region.length) {
					this.FormData.region = this.FormData.region[this.FormData.region.length - 1]
				}
				this.FormData.unit = {
					id: this.FormData.unit.id
				}
				var res = await this.$API.enterprise.store.save.post(this.FormData);
				this.isSaveing = false;
				if (res.code == 200) {
					// if (this.mode == 'add') {
					// 	this.FormData.id = res.data
					// }
					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				Object.assign(this.FormData, data)
				delete this.FormData.properties
				delete this.FormData.createTime
				delete this.FormData.lastModifyTime
				delete this.FormData.auditingTime
				delete this.FormData.updateByName
				delete this.FormData.createByName
				delete this.FormData.auditing
				delete this.FormData.version
				delete this.FormData.auditingByName
				delete this.FormData.createBy
				delete this.FormData.deleted
				delete this.FormData.auditingBy
				delete this.FormData.updateBy
				this.FormData.unit.id = data.unitId
				this.FormData.unit.name = data.unitName
				this.showAMap = false
			}
		}
	}
</script>

<style>
</style>
